package com.citywy.test;

import com.citywy.dao.UserDao;
import com.citywy.domain.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.InputStream;

public class UserTest2 {
    private InputStream in;
    private SqlSessionFactory factory;

    @Before
    public void init() throws Exception{
        in = Resources.getResourceAsStream("SqlMapConfig.xml");
        factory = new SqlSessionFactoryBuilder().build(in);
    }

    @After
    public void destroy() throws Exception{
        in.close();
    }

    //测试二级缓存
    @Test
    public void testFirstLevelCache(){
        SqlSession session1 = factory.openSession();
        UserDao u1 = session1.getMapper(UserDao.class);
        User user1=u1.findById(2);
        System.out.println(user1);
        session1.close();//一级缓存消失

        SqlSession session2 = factory.openSession();
        UserDao u2 = session2.getMapper(UserDao.class);
        User user2 = u2.findById(2);
        System.out.println(user2);
        session2.close();

        System.out.println(user1+"--"+user2);
    }
}
